package com.ez.supervise.action.biz;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.ez.supervise.action.base.BaseJsonAction;
import com.ez.supervise.core.Page;
import com.ez.supervise.core.Result;
import com.ez.supervise.model.biz.DemocracyScore;
import com.ez.supervise.service.DemocracyScoreService;
import com.ez.supervise.util.JsonUtils;

public class DemocracyScoreAction extends BaseJsonAction {
    private static final long serialVersionUID = 8735170970520475073L;
    private Long id;
    private DemocracyScoreService democracyScoreService;
    private DemocracyScore democracyScore;
    private Page page;

    public String saveDemocracyScore() {
	try {
	    if (democracyScore.isNew()) {
		democracyScoreService.insert(democracyScore);
	    } else {
		democracyScoreService.update(democracyScore);
	    }
	    Result result = new Result(democracyScore.getId());
	    result.setEntity(democracyScore);
	    JsonConfig config = new JsonConfig();
	    config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new",
		"content" });
	    outString(JSONObject.fromObject(result, config).toString());
	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    public String findAllDemocracyScore() {
	try {
	    page = new Page();
	    List result = democracyScoreService.findAll();
	    page.setRoot(result);
	    page.setTotalProperty(result.size());
	    outJsonString(JsonUtils.fromObject(page, new String[] { "creator", "createTime", ",modifier", "modifyTime",
		"isDeleted", "new" }));
	    
	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    public String findAll() {
	page = new Page();
	page.setTotalProperty(0);
	page.setRoot(democracyScoreService.findAll());

	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new", });

	JSONObject jsonObj = JSONObject.fromObject(page, config);
	// System.out.println(jsonObj.toString());
	outJsonString(jsonObj.toString());
	return SUCCESS;
    }

    /**
     * @return
     */
    public String deleteDemocracyScore() {
	try {
	    if (id != null && id > 0) {
		democracyScoreService.delete(id);
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    /**
     * @return
     * @throws Exception
     */
    public String updateDemocracyScore() throws Exception {
	String fieldName = getRequest().getParameter("fieldName");
	String fieldValue = getRequest().getParameter("fieldValue");
	String strDemocracyScoreId = getRequest().getParameter("id");
	try {
	    if (id != null && id > 0) {
		DemocracyScore saveDemocracyScore = new DemocracyScore();
		saveDemocracyScore.setId(id);
		BeanUtils.setProperty(saveDemocracyScore, fieldName, fieldValue);
		democracyScoreService.update(saveDemocracyScore);
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}

	return SUCCESS;
    }

    public Long getId() {
	return id;
    }

    public void setId(Long id) {
	this.id = id;
    }

    public static void main(String[] args) {
	DemocracyScore democracyScore = new DemocracyScore();
	democracyScore.setId(1L);
	// democracyScore.setName("linlinyu");
	JSONObject json = JSONObject.fromObject(democracyScore);

	System.out.println(json);

	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new", });

	JSONObject jsonObj = JSONObject.fromObject(democracyScore, config);
	System.out.println(jsonObj.toString());
    }

    public void setDemocracyScoreService(DemocracyScoreService democracyScoreService) {
	this.democracyScoreService = democracyScoreService;
    }

    public DemocracyScore getDemocracyScore() {
	return democracyScore;
    }

    public void setDemocracyScore(DemocracyScore democracyScore) {
	this.democracyScore = democracyScore;
    }

    public Page getPage() {
	return page;
    }

    public void setPage(Page page) {
	this.page = page;
    }

}
